package models.mappers;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import enums.WdOrderTaskStatus;
import models.WdOrderTask;
import vos.WdOrderTaskSearchVo;

/**
 * 微代任务订单任务
 * @author zhiyuan
 * @version v1.0
 * @since 2017年4月1日 下午9:29:03
 */
public interface WdOrderTaskMapper {

	@Update("update wd_order_task set status='WAIT_CHECK' where task_id=#{id} and status='WAIT_SUBMIT'")
	void submitTask(@Param("id") Long id);
	
	@Select("select count(1) from wd_order_task S inner join wd_task M on S.task_id=M.id where M.seller_id = #{sellerId} and S.status='WAIT_CHECK'")
	Integer countWaitCheckTask(@Param("sellerId") Long sellerId);

	@Insert("insert into wd_order_task(task_id, share_id, buyer_id, order_no, status, create_time) values(#{taskId}, #{shareId}, #{buyerId}, #{orderNo}, #{status}, #{createTime})")
	void insert(WdOrderTask obj);
	
	@Select("select * from wd_order_task where order_no = #{orderNo}")
	WdOrderTask getTaskByOrderNo(@Param("orderNo") String orderNo);
	
	Integer countTask(WdOrderTaskSearchVo vo);
	
	List<WdOrderTask> listTask(WdOrderTaskSearchVo vo);
	
	@Select("select * FROM wd_order_task WOT inner join wd_task WT ON WT.ID = WOT.task_id  where WOT.id=#{id} and WT.seller_id = #{sellerId} and WOT.status='WAIT_CHECK'")
	WdOrderTask getWaitCheckTask(@Param("id") Long id, @Param("sellerId") Long sellerId);
	
	@Update("update wd_order_task set status=#{status}, modify_time=sysdate() where id=#{id}")
	void checkOrder(@Param("status") WdOrderTaskStatus status,@Param("id") Long id);

	@Delete("delete from wd_order_task where id=#{id} and buyer_id=#{buyerId} and status='DISALLOWED'")
	Integer delOrder(@Param("id") Long orderId,@Param("buyerId") Long buyerId);
	
	@Update("update wd_order_task set order_no = #{orderNo}, status='WAIT_CHECK' where id=#{id} and buyer_id = #{buyerId} and status='DISALLOWED'")
	Integer updateOrder(@Param("id") Long orderId, @Param("orderNo") String orderNo, @Param("buyerId") Long buyerId);

	@Select("select count(1) from wd_order_task where order_no = #{orderNo}")
	Integer countByOrderNo(@Param("orderNo") String orderNo);

	@Select("select id, order_no, status from wd_order_task where share_id=#{shareId} and buyer_id = #{buyerId}")
	List<WdOrderTask> listWdOrderByShareId(@Param("shareId") Long taskId, @Param("buyerId") long buyerId);

	@Select("SELECT count(1) FROM wd_order_task WHERE share_id = #{shareId} and status = 'DISALLOWED' ")
	Integer countDisallowed(@Param("shareId") Long id);
}
